/*
 * Copyright 2014 Semidata. All rights reserved.
 */
package com.semidata.rtd.engine.job;

import com.semidata.rtd.engine.executor.TaskExecutor;

/**
 * @author shewei@semidata.com, She Wei
 * 
 */
public abstract class AbstractJob implements Job {

	private TaskExecutor executor;
	private JobCompleteHandler doneHandler;
	private JobCompleteHandler errorHandler;
	private boolean complete = false;
	
	public AbstractJob(TaskExecutor executor, JobCompleteHandler doneHandler, JobCompleteHandler errorHandler) {
		this.executor = executor;
		this.doneHandler = doneHandler;
		this.errorHandler = errorHandler;
	}
	
	public TaskExecutor getExecutor() {
		return executor;
	}
	

	@Override
	public void start() {
		schedule(null);
	}

	@Override
	public void done() {
		complete = true;
		if (doneHandler != null)
			doneHandler.handle();
	}

	@Override
	public void error() {
		complete = true;
		if (errorHandler != null)
			errorHandler.handle();
	}

	@Override
	public void taskComplete(Task task) {
		schedule(task);
	}

	protected abstract void schedule(Task task);
	
	public boolean isDone() {
		return complete;
	}

}
